extends GridContainer
const SLOT = preload("res://Scenes/slot.tscn")
@export var type:String
@export var main:Main

var button_group:ButtonGroup

func _ready():
	# 创建按钮组
	button_group = ButtonGroup.new()
	button_group.allow_unpress = true
	button_group.pressed.connect(_on_button_group_pressed)


func init():
	# 清除现有的子节点
	for child in get_children():
		child.queue_free()
	
	# 从main中获取库存数据
	if main and main.inventory.size() > 0:
		for skin_state in main.inventory:
			if skin_state.skin_profile.type == type:
				# 创建新的皮肤槽位
				var slot = SLOT.instantiate()
				slot.skin_state = skin_state
				# 连接槽位的信号
				slot.button_toggled.connect(slot_button_toggled)
				# 将槽位添加为子节点
				add_child(slot)
				# 将按钮添加到按钮组
				slot.texture_button.button_group = button_group

func update_all():
	for child in get_children():
		child._ready()

func slot_button_toggled(toggled:bool,slot_skin:SkinState):
	slot_skin.is_equipped = toggled
	#if toggled:
		#print("加载"+slot_skin.skin_profile.skin_name)
		#main.update_skin(type,slot_skin)
	#else:
		#print("卸载"+slot_skin.skin_profile.skin_name)
		#main.update_skin(type,null)


func _on_button_group_pressed(_button):
	var pressed_button = button_group.get_pressed_button()
	if pressed_button:
		var pressed_slot:SlotUI = pressed_button.get_parent()
		main.change_skin(type,pressed_slot.skin_state)
	else:
		main.change_skin(type,null)
